package top.kaoshanji.p2ch03a;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import top.kaoshanji.p2ch03a.mapper.UserQueryMapper;
import top.kaoshanji.p2ch03a.model.User;
import top.kaoshanji.p2ch03a.util.SqlSessionUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试 UserMapper
 * @author kaoshanji
 * ^_^
 * create time 2025/4/17 21:20
 */
public class UserQueryMapperTest {

    /**
     * 代码清单test-p2ch03a-51页a：统计记录条数
     */
    @Test
    public void testGetUserCount() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserQueryMapper userQueryMapper = sqlSession.getMapper(UserQueryMapper.class);

            Integer count = userQueryMapper.getUserCount();
            System.out.println("用户总量：" + count);
        } finally {
            SqlSessionUtil.closeSqlSession(sqlSession);
        }
    }

    /**
     * 代码清单test-p2ch03a-52页a：只有一条数据，使用Map返回
     */
    @Test
    public void testQueryUserMapById() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserQueryMapper userQueryMapper = sqlSession.getMapper(UserQueryMapper.class);

            Map<String, Object> map = userQueryMapper.queryUserMapById(1);
            for (Map.Entry<String, Object> entry: map.entrySet()) {
                System.out.println("key: " + entry.getKey() + " ,value: " + entry.getValue());
            }
        } finally {
            SqlSessionUtil.closeSqlSession(sqlSession);
        }
    }

    /**
     * 代码清单test-p2ch03a-53页a：查询多条数据，返回Map集合
     */
    @Test
    public void testQueryAllUserMap() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserQueryMapper userQueryMapper = sqlSession.getMapper(UserQueryMapper.class);

            Map<Integer, User> userMap = userQueryMapper.queryAllUserMap();
            for (Map.Entry<Integer, User> entry: userMap.entrySet()) {
                System.out.println("key: " + entry.getKey() + " ,value: " + entry.getValue());
            }
        } finally {
            SqlSessionUtil.closeSqlSession(sqlSession);
        }
    }

    /**
     * 代码清单test-p2ch03a-65页a：接口方法的参数是Map类型
     */
    @Test
    public void testQueryUsersByMap() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserQueryMapper userQueryMapper = sqlSession.getMapper(UserQueryMapper.class);

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("username", "lucy");
            paramMap.put("gender", "gender");

            List<User> userList = userQueryMapper.queryUsersByMap(paramMap);
            for (User user: userList) {
                System.out.println(user);
            }

        } finally {
            SqlSessionUtil.closeSqlSession(sqlSession);
        }
    }

    /**
     * 代码清单test-p2ch03a-66页a：接口方法的参数是List类型
     */
    @Test
    public void testQueryUsersByList() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserQueryMapper userQueryMapper = sqlSession.getMapper(UserQueryMapper.class);

            List<Integer> idList = new ArrayList<>();
            idList.add(1);
            idList.add(2);
            idList.add(3);

            List<User> userList = userQueryMapper.queryUsersByList(idList);
            for (User user: userList) {
                System.out.println(user);
            }
        } finally {
            SqlSessionUtil.closeSqlSession(sqlSession);
        }
    }

    /**
     * 代码清单test-p2ch03a-68页a：接口方法的参数是数组类型
     */
    @Test
    public void testQueryUsersByArray() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserQueryMapper userQueryMapper = sqlSession.getMapper(UserQueryMapper.class);

            int [] idArray = {1, 2, 3};
            List<User> userList = userQueryMapper.queryUsersByArray(idArray);
            for (User user: userList) {
                System.out.println(user);
            }
        } finally {
            SqlSessionUtil.closeSqlSession(sqlSession);
        }
    }

    /**
     * 代码清单test-p2ch03a-72页a：传递表名获取总数
     */
    @Test
    public void testGetCount() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserQueryMapper userQueryMapper = sqlSession.getMapper(UserQueryMapper.class);

            String tableName = "user";
            Integer count = userQueryMapper.getCount(tableName);
            System.out.println(tableName + " 表的总数： " + count);
        } finally {
            SqlSessionUtil.closeSqlSession(sqlSession);
        }
    }

    /**
     * 代码清单test-p2ch03a-74页a：传递字符串格式的多个id值
     */
    @Test
    public void testDeleteMore() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserQueryMapper userQueryMapper = sqlSession.getMapper(UserQueryMapper.class);

            String ids = "7, 8, 9";
            int result = userQueryMapper.deleteMore(ids);
            System.out.println("删除的数据行数：" + result);

        } finally {
            SqlSessionUtil.closeSqlSession(sqlSession);
        }
    }


}
